//package com.example.springboottest.example.soap;
//
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.UnsupportedEncodingException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//import javax.xml.parsers.DocumentBuilder;
//import javax.xml.parsers.DocumentBuilderFactory;
//import javax.xml.parsers.ParserConfigurationException;
//
//
//import org.apache.http.HttpEntity;
//import org.apache.http.HttpResponse;
//import org.apache.http.NameValuePair;
//import org.apache.http.client.ClientProtocolException;
//import org.apache.http.client.HttpClient;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.impl.client.DefaultHttpClient;
//import org.apache.http.impl.client.HttpClients;
//import org.apache.http.message.BasicNameValuePair;
//import org.apache.http.util.EntityUtils;
//import org.dom4j.DocumentException;
//import org.dom4j.DocumentHelper;
//
//public class HttpClientTest {
//    //字符编码及其他参数
//    private static final String charset = "utf-8";
//    private static final String url = "http://127.0.0.1:8080/WebAPI/dtest.actioin";
//
//
//    public static void main(String[] args) {
//        HttpClientTest client = new HttpClientTest();
//    }
//
//    //post方式请求
//    public static Map<String, String> sendMessageByPost(String content, String phones) {
//        Map<String, String> map = new HashMap<String, String>();
//        // 创建默认的httpClient实例.
//        CloseableHttpClient httpclient = HttpClients.createDefault();
//        // 创建httppost
//        HttpPost httppost = new HttpPost(url);
//        // 创建参数队列
//        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
//        formparams.add(new BasicNameValuePair("user", username));
//        formparams.add(new BasicNameValuePair("pwd", password));
//        formparams.add(new BasicNameValuePair("mobiles", phones));
//        formparams.add(new BasicNameValuePair("contents", content));
//
//        UrlEncodedFormEntity uefEntity;
//        try {
//            uefEntity = new UrlEncodedFormEntity(formparams, charset);
//            httppost.setEntity(uefEntity);
//            System.out.println("executing request " + httppost.getURI());
//            CloseableHttpResponse response = httpclient.execute(httppost);
//            try {
//                HttpEntity entity = response.getEntity();
//                if (entity != null) {
//                    //将返回的数据直接转成String
//                    String str = EntityUtils.toString(entity, "UTF-8");
//                    System.out.println("--------------------------------------");
//                    //注意这里不能写成EntityUtils.toString(entity, "UTF-8"),因为EntityUtils只能调用一次，否则会报错：java.io.IOException: Attempted read from closed stream
//                    System.out.println("Response content: " + str);
//                    System.out.println("--------------------------------------");
//
//                    //这里用str作为参数获得 Document 对象
//                    org.dom4j.Document document = DocumentHelper.parseText(str);
//                    org.dom4j.Element rootElement = document.getRootElement();
//
//                    String code = rootElement.element("Code").getText();
//                    String result = rootElement.element("Result").getText();
//                    map.put("Code", code);
//                    map.put("Result", result);
//                }
//            } catch (DocumentException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            } finally {
//                response.close();
//            }
//
//        } catch (ClientProtocolException e) {
//            e.printStackTrace();
//        } catch (UnsupportedEncodingException e1) {
//            e1.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            // 关闭连接,释放资源
//            try {
//                httpclient.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//
//        return map;
//    }
//}
